import DC from '../utils/dictionary.json';
Date.prototype.Format = function (fmt) {
  var o = {
    "M+": this.getMonth() + 1, //月份
    "d+": this.getDate(), //日
    "h+": this.getHours(), //小时
    "m+": this.getMinutes(), //分
    "s+": this.getSeconds(), //秒
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
    "S": this.getMilliseconds() //毫秒
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}
export default {
  /**
   * 转换成正常时间
   * @param {*} format
   */
  format(format) {
    var date = new Date(format);
    var y = 1900 + date.getYear();
    var m = "0" + (date.getMonth() + 1);
    var d = "0" + date.getDate();
    return y + "-" + m.substring(m.length - 2, m.length) + "-" + d.substring(d.length - 2, d.length);
  },

  /**
   * 验证身份证长度和类型
   * @param {*} card
   */
  isCardNo(card) {
    //身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X
    let reg = /(^\d{15}$)|(^\d{17}(\d|X)$)/;
    if (!reg.test(card)) {
      return false;
    }
    return true;
  },

  /**
   * 验证有效手机
   * @param {*} phone
   */
  isPhone(phone) {
    let reg = /^1[3|4|5|6|7|8|9][0-9]{9}$/;
    if (!reg.test(phone)) {
      return false;
    }
    return true;
  },

  /**
   * 判断是否金额
   * @param {*} money
   */
  isMoney(money) {
    let reg = /^\d+\.?\d{0,2}$/
    if (!reg.test(money)) {
      return false;
    }
    return true;
  },

  /**
   * 判断是否数字
   * @param {*} number
   */
  isNumber(number) {
    let reg = /^[0-9]+.?[0-9]*/;
    if (!reg.test(number)) {
      return false;
    }
    return true;
  },

  /**
   * 获取URL参数
   * @param {*} paraName
   */
  GetUrlParam(paraName) {
    var url = document.location.toString();
    var arrObj = url.split("?");

    if (arrObj.length > 1) {
      var arrPara = arrObj[1].split("&");
      var arr;

      for (var i = 0; i < arrPara.length; i++) {
        arr = arrPara[i].split("=");

        if (arr != null && arr[0] == paraName) {
          return arr[1];
        }
      }
      return "";
    }
    else {
      return "";
    }
  },
  /**
   * 获取URL参数
   * @param {*} paraName
   */
  GetUrlParams(paraName) {
    var reg = new RegExp("(^|&)" + paraName + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
  },

  /**
   * YYYY-MM-DD 转 yyyymmdd
   * @param {*} DateStr
   */
  ConverToDate(DateStr) {
    if (/^\d{4}-\d{2}-\d{2}$/.test(DateStr)) {
      var date = DateStr;
      DateStr = date.replace(/\-/g, "");
    }
    return DateStr;
  },

  /**
   *  Date转yyyymmdd
   * @param {*} DateStr
   */
  DateTostr(date) {
    if (date) {
      var date = new Date(date);
      var DateStr = date.getFullYear() + ""
        + ((date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1)) + ""
        + (date.getDate() > 9 ? date.getDate() : "0" + date.getDate())
      return DateStr;
    }
    return ""
  },
  /**
   *  Date转yyyy-mm-dd
   * @param {*} DateStr
   */
  _DateTostr(date) {
    if (date) {
      var date = new Date(date);
      var DateStr = date.getFullYear() + "-"
        + ((date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1)) + "-"
        + (date.getDate() > 9 ? date.getDate() : "0" + date.getDate())
      return DateStr;
    }
    return ""
  },

  /**
   * yyymmmdd转yyyy-mm-dd
   * @param {*} str
   */
  formatDate(str) {
    return str && str.substring(0, 4) + '-' + str.substring(4, 6) + '-' + str.substring(6, 8) || '';
  },

  //验证身份证
  isIdCard(card) {
    let aCity = {
      11: "北京",
      12: "天津",
      13: "河北",
      14: "山西",
      15: "内蒙古",
      21: "辽宁",
      22: "吉林",
      23: "黑龙江",
      31: "上海",
      32: "江苏",
      33: "浙江",
      34: "安徽",
      35: "福建",
      36: "江西",
      37: "山东",
      41: "河南",
      42: "湖北",
      43: "湖南",
      44: "广东",
      45: "广西",
      46: "海南",
      50: "重庆",
      51: "四川",
      52: "贵州",
      53: "云南",
      54: "西藏",
      61: "陕西",
      62: "甘肃",
      63: "青海",
      64: "宁夏",
      65: "新疆",
      71: "台湾",
      81: "香港",
      82: "澳门",
      91: "国外"
    }
    let iSum = 0;
    if (!/^\d{17}(\d|x)$/i.test(card)) return false;
    card = card.replace(/x$/i, "a");
    if (aCity[parseInt(card.substr(0, 2))] == null) return false;
    let sBirthday = card.substr(6, 4) + "-" + Number(card.substr(10, 2)) + "-" + Number(card.substr(12, 2));
    let d = new Date(sBirthday.replace(/-/g, "/"));
    if (sBirthday != (d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate())) return false;
    for (let i = 17; i >= 0; i--) iSum += (Math.pow(2, i) % 11) * parseInt(card.charAt(17 - i), 11);
    if (iSum % 11 != 1) return false;
    return true;
  },
  /**
   * 通过编码获取代码中文值
   * field字段，code代码值
   */
  decompile(field, code){
    if (code) {
      let name = DC[field].filter(a => a.code == code)[0].name;
      if (name) {
        return name;
      } else {
        return "";
      }
    } else {
      return "";
    }
  },

  getInfo(){
    let data = sessionStorage.getItem("userInfo");
    if (data) {
      return JSON.parse(data);
    } else return ""
  },

//电话号码脱敏
  replaceTel(phone){
    if (phone) {
      return phone.replace(/(.{3}).*(.{4})/, "$1****$2");
    } else {
      return null;
    }
  },
//身份证脱敏
  replaceCard(card){
    if (card) {
      return card.replace(/(.{1}).*(.{1})/, "$1**********$2");
    } else {
      return null;
    }
  },
//姓名脱敏
  replaceName(name){
    if (name) {
      let result = '*' + name.substring(1, name.length);
      return result;
    } else {
      return null;
    }
  },
//社保卡脱敏
  replaceSbk(card){
    if (card) {
      return card.replace(/(.{1}).*(.{1})/, "$1******$2");
    } else {
      return null;
    }
  },
  /**
   * 保存token
   */
  setToken(token) {
    if (!token) {
      sessionStorage.removeItem("token")
      setUser()
    } else {
      sessionStorage.setItem("token", token)
    }
  },

  /**
   * 获取token
   * @returns {string}
   */
 getToken() {
    return sessionStorage.getItem("token");

  },

  /**
   * 保存用户信息
   * idName,idNo
   */
 setUser(user) {
    if (!user) {
      sessionStorage.removeItem("userInfo")
    } else {
      sessionStorage.setItem("userInfo", JSON.stringify(user))
    }

  },

  /**
   * 获取用户信息
   * @returns {string}
   */
 getUser() {
    let user = sessionStorage.getItem("userInfo");
    if (!user) {
      return null;
    }
    return JSON.parse(user);
  },
}
